package com.fourforfo.fourmall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.fourforfo.fourmall.product.vo.SpuSaveVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.fourforfo.fourmall.product.entity.SpuInfoEntity;
import com.fourforfo.fourmall.product.service.SpuInfoService;
import com.fourforfo.common.utils.PageUtils;
import com.fourforfo.common.utils.R;



/**
 * spu信息
 *
 * @author fourforfo
 * @email fourforfo@gmail.com
 * @date 2022-06-16 12:30:14
 */
@RestController
@RequestMapping("product/spuinfo")
public class SpuInfoController {
    @Autowired
    private SpuInfoService spuInfoService;

    /**
     * 根据skuIds获取所有spuInfo信息，将spuInfo信息和对应的skuId保存到map中
     * @param skuIds
     * @return
     */
    @PostMapping("/listSpuInfo")
    public R listSpuInfoBySkuIds(@RequestBody List<Long> skuIds){
        Map<Long,SpuInfoEntity> map = spuInfoService.listSpuInfoBySkuIds(skuIds);

        return R.ok().setData(map);
    }

    /**
     *  1. 商品上架即更新商品状态
     *  2. 将该商品信息加入到ElasticSearch
     * @param spuId pms_spu_info表的id
     */
    @RequestMapping("/{spuId}/up")
    public R up(@PathVariable("spuId") Long spuId){

        spuInfoService.upProduct(spuId);

        return R.ok();
    }


    /**
     * 检索关键字：key，catelogId，brandId
     */
    @RequestMapping("/list")
//    @RequiresPermissions("product:spuinfo:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = spuInfoService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
//    @RequiresPermissions("product:spuinfo:info")
    public R info(@PathVariable("id") Long id){
		SpuInfoEntity spuInfo = spuInfoService.getById(id);

        return R.ok().put("spuInfo", spuInfo);
    }

    /**
     * 保存发布商品业务所提交的所有相关信息
     */
    @RequestMapping("/save")
//    @RequiresPermissions("product:spuinfo:save")
    public R save(@RequestBody SpuSaveVO spuSaveVO){
        spuInfoService.saveSpuInfo(spuSaveVO);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
//    @RequiresPermissions("product:spuinfo:update")
    public R update(@RequestBody SpuInfoEntity spuInfo){
		spuInfoService.updateById(spuInfo);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
//    @RequiresPermissions("product:spuinfo:delete")
    public R delete(@RequestBody Long[] ids){
		spuInfoService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
